﻿using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Collections;
using System;
using System.IO;


namespace Business {
    public class LineChart {
        public Bitmap Bitmapb;
        public string strTitle = "Trend Report";
        public ArrayList ArrayListchartValues = new ArrayList();
        public ArrayList ArrayListlabelStrings = new ArrayList();
        public float ftXorigin = 0, ftYorigin = 0;
        public float ftScaleX, ftScaleY;
        public float ftXdivs = 2, ftYdivs = 2;
        private int intWidth, intHeight;
        private Graphics Graphicsg;
        System.Web.UI.Page Pagep;

        struct datapoint {
            public float ftx;
            public float fty;
            public bool bolvalid;
            //public string label;
        }
        //initialize

        public LineChart(int intmyWidth, int intmyHeight, System.Web.UI.Page myPage) {
            intWidth = intmyWidth; intHeight = intmyHeight;
            ftScaleX = intmyWidth; ftScaleY = intmyHeight;
            Bitmapb = new Bitmap(intmyWidth, intmyHeight);
            Graphicsg = Graphics.FromImage(Bitmapb);
            Pagep = myPage;
            ArrayListlabelStrings.Add("");
        }
        public void AddValue(int intx, int inty, string label) {
            datapoint myPoint;
            myPoint.ftx = intx;
            myPoint.fty = inty;
            myPoint.bolvalid = true;
            ArrayListchartValues.Add(myPoint);
            ArrayListlabelStrings.Add(label);

        }
        public void Draw() {

            int inti;
            float ftx, fty, ftx0, fty0;
            string strmyLabel;
            Pen penblackPen = new Pen(Color.Black, 1);
            Pen penredPen = new Pen(Color.Red, 1);
            Brush BrushblackBrush = new SolidBrush(Color.Black);
            Brush BrushgreenBrush = new SolidBrush(Color.Green);
            Font FontaxesFont = new Font("arial", 10);

            //first establish working area

            Pagep.Response.ContentType = "image/jpeg";
            Graphicsg.FillRectangle(new
            SolidBrush(Color.LightYellow), 0, 0, intWidth, intHeight);
            int intChartInset = 30;// 50;
            int intChartWidth = intWidth - (2 * intChartInset);
            int intChartHeight = intHeight - (2 * intChartInset);
            Graphicsg.DrawRectangle(new Pen(Color.Black, 1), intChartInset, intChartInset, intChartWidth, intChartHeight);
            //must draw all text items before doing the rotate below
            Graphicsg.DrawString(strTitle, new Font("arial", 12), BrushblackBrush, (intWidth / 3), 8);

            //draw X axis labels
            ArrayListlabelStrings.Add("");
            for (inti = 0; inti <= ftXdivs; inti++) {

                ftx = intChartInset + (inti * intChartWidth) / ftXdivs;
                fty = intChartHeight + intChartInset;
                //strmyLabel = (ftXorigin + (ftScaleX * inti / ftXdivs)).ToString();
                strmyLabel = ArrayListlabelStrings[inti].ToString(); //(inti ).ToString();
                Graphicsg.DrawString(strmyLabel, FontaxesFont, BrushblackBrush, ftx - 24, fty + 10);
                Graphicsg.DrawLine(penblackPen, ftx, fty + 2, ftx, fty - 2);
            }

            //draw Y axis labels
            for (inti = 0; inti <= ftYdivs; inti++) {

                ftx = intChartInset;
                fty = intChartHeight + intChartInset - (inti * intChartHeight / ftYdivs);
                strmyLabel = (ftYorigin + (ftScaleY * inti / ftYdivs)).ToString();
                Graphicsg.DrawString(strmyLabel, FontaxesFont, BrushblackBrush, 5, fty - 6);
                Graphicsg.DrawLine(penblackPen, ftx + 2, fty, ftx - 2, fty);

            }



            //transform drawing coords to lower-left (0,0)

            Graphicsg.RotateTransform(180);
            Graphicsg.TranslateTransform(0, -intHeight);
            Graphicsg.TranslateTransform(-intChartInset, intChartInset);
            Graphicsg.ScaleTransform(-1, 1);



            //draw chart data

            datapoint datapointprevPoint = new datapoint();
            datapointprevPoint.bolvalid = false;

            foreach (datapoint myPoint in ArrayListchartValues) {

                if (datapointprevPoint.bolvalid == true) {

                    ftx0 = intChartWidth * (datapointprevPoint.ftx - ftXorigin) / ftScaleX;
                    fty0 = intChartHeight * (datapointprevPoint.fty - ftYorigin) / ftScaleY;
                    ftx = intChartWidth * (myPoint.ftx - ftXorigin) / ftScaleX;
                    //ftx = intChartInset + (myPoint.ftx * intChartWidth) / ftXdivs;

                    fty = intChartHeight * (myPoint.fty - ftYorigin) / ftScaleY;
                    Graphicsg.DrawLine(penredPen, ftx0, fty0, ftx, fty);
                    Graphicsg.FillEllipse(BrushgreenBrush, ftx0 - 2, fty0 - 2, 4, 4);
                    Graphicsg.FillEllipse(BrushgreenBrush, ftx - 2, fty - 2, 4, 4);

                }

                datapointprevPoint = myPoint;

            }

            //finally send graphics to browser
            Bitmapb.Save(Pagep.Response.OutputStream, ImageFormat.Jpeg);


        }
        LineChart() {

            Graphicsg.Dispose();

            Bitmapb.Dispose();

        }

    }
}